package restaurant.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

/**
 * This JPanel displays the main actions a manager can perform.
 * 
 * @author Odessa
 */
@SuppressWarnings("serial")
public class ActionsPanel extends JPanel
{
	/** The ADD Constant. */
	public final static int ADD = 1;

	/** The EDIT Constant. */
	public final static int EDIT = 2;

	/** The add menu item radio button. */
	private JRadioButton addItemRadio;

	/** The browse menu radio button. */
	private JRadioButton browseMenuRadio;

	/** The menu item availability radio button. */
	private JRadioButton availabilityRadio;

	/** The add offer radio button. */
	private JRadioButton addOfferRadio;

	/** The view offers radio button. */
	private JRadioButton viewOffersRadio;

	/** The view ratings radio button. */
	private JRadioButton ratingsRadio;

	/** The view comments radio button. */
	private JRadioButton commentsRadio;

	/** The view tables data radio button. */
	private JRadioButton tablesRadio;

	/** The view kitchen queue radio button. */
	private JRadioButton kitchenRadio;

	/** The view customer queue radio button. */
	private JRadioButton customerRadio;

	/** The view items sold radio button. */
	private JRadioButton itemsSoldRadio;



	/**
	 * Creates the panel.
	 */
	public ActionsPanel() {
		setLayout(new BorderLayout(0, 0));

		JLabel descLabel = new JLabel("What would you like to do?");
		descLabel.setIcon(new ImageIcon("resources/todo.png"));
		descLabel.setVerticalAlignment(SwingConstants.TOP);
		descLabel.setHorizontalAlignment(SwingConstants.CENTER);
		descLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
		add(descLabel, BorderLayout.NORTH);

		JPanel radioPanel = new JPanel();
		add(radioPanel, BorderLayout.CENTER);
		radioPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		// Create the menu options panel and radio buttons
		JPanel menuPanel = new JPanel();
		menuPanel.setBorder(new TitledBorder(null, "Menu Options",
				TitledBorder.CENTER, TitledBorder.TOP, null, null));
		radioPanel.add(menuPanel);
		menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));

		addItemRadio = new JRadioButton("Add menu item");
		browseMenuRadio = new JRadioButton("Browse menu");
		availabilityRadio = new JRadioButton("Set availability");
		menuPanel.add(addItemRadio);
		menuPanel.add(browseMenuRadio);
		menuPanel.add(availabilityRadio);

		// Create the offer options panel and radio buttons
		JPanel offerPanel = new JPanel();
		offerPanel.setBorder(new TitledBorder(null, "Offer Options",
				TitledBorder.CENTER, TitledBorder.TOP, null, null));
		radioPanel.add(offerPanel);
		offerPanel.setLayout(new BoxLayout(offerPanel, BoxLayout.Y_AXIS));

		addOfferRadio = new JRadioButton("Add offer");
		viewOffersRadio = new JRadioButton("View offers");
		offerPanel.add(addOfferRadio);
		offerPanel.add(viewOffersRadio);

		// Create the report panel and radio buttons
		JPanel reportPanel = new JPanel();
		reportPanel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Reports",
				TitledBorder.CENTER, TitledBorder.TOP, null, null));
		radioPanel.add(reportPanel);
		reportPanel.setLayout(new BoxLayout(reportPanel, BoxLayout.Y_AXIS));

		ratingsRadio = new JRadioButton("View item ratings");
		commentsRadio = new JRadioButton("View comments");
		tablesRadio = new JRadioButton("View table usage");
		kitchenRadio = new JRadioButton("View kitchen queue");
		customerRadio = new JRadioButton("View customer wait queue");
		itemsSoldRadio = new JRadioButton("View items sold");
		reportPanel.add(ratingsRadio);
		reportPanel.add(commentsRadio);
		reportPanel.add(tablesRadio);
		reportPanel.add(kitchenRadio);
		reportPanel.add(customerRadio);
		reportPanel.add(itemsSoldRadio);

		// Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(addItemRadio);
		group.add(browseMenuRadio);
		group.add(availabilityRadio);
		group.add(addOfferRadio);
		group.add(viewOffersRadio);
		group.add(ratingsRadio);
		group.add(commentsRadio);
		group.add(tablesRadio);
		group.add(kitchenRadio);
		group.add(customerRadio);
		group.add(itemsSoldRadio);

		// Create the panel for the JButtons and add the buttons
		JPanel buttonPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) buttonPanel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		add(buttonPanel, BorderLayout.SOUTH);

		JButton okButton = new JButton("OK");
		okButton.setPreferredSize(new Dimension(73, 26));
		okButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt)
			{
				okButtonActionPerformed(evt);
			}
		});
		buttonPanel.add(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.setPreferredSize(new Dimension(73, 26));
		cancelButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt)
			{
				System.exit(0);
			}
		});
		buttonPanel.add(cancelButton);

	}



	/**
	 * The event handler for the OK button.
	 * 
	 * @param evt the action event.
	 */
	private void okButtonActionPerformed(ActionEvent evt)
	{
		Frame frame = null;

		// TODO add your handling code here:
		if (addItemRadio.isSelected()) {
			frame = new Frame(new MenuItemPanel());
			frame.setTitle(addItemRadio.getText());
			frame.setIconImage(Restaurant.MENU.getImage());
		}
		else if (browseMenuRadio.isSelected()) {
			JFrame root = (JFrame) SwingUtilities.getRoot(this);
			frame = new Frame(new MenuPanel(root));
		}
		else if (availabilityRadio.isSelected()) {
		}
		else if (addOfferRadio.isSelected()) {
			frame = new Frame(new OfferPanel());
			frame.setTitle(addOfferRadio.getText());
		}
		else if (viewOffersRadio.isSelected()) {
			frame = new Frame(new ViewOffersPanel());
			frame.setTitle("Offers");
		}
		else if (ratingsRadio.isSelected()) {
		}
		else if (commentsRadio.isSelected()) {
		}
		else if (tablesRadio.isSelected()) {
			// String empty = Restaurant.actions.viewAvailableTables();
			// String occupied = Restaurant.actions.viewOccupiedTables();
			// String waiting = Restaurant.actions.viewWaitingTables();

			int numEmpty = 0;
			int numOccupied = 0;
			int numWaiting = 0;

			frame = new Frame(new TableDataPanel(numEmpty, numOccupied,
					numWaiting));
			frame.setTitle("Table usage data");
		}
		else if (kitchenRadio.isSelected()) {
		}
		else if (customerRadio.isSelected()) {
		}
		else if (itemsSoldRadio.isSelected()) {
		}

		frame.run();
	}
}
